package com.designCreator.view
{
	import com.designCreator.command.FabricDesignsCommand;
	import com.designCreator.controls.FabricTabContent;
	import com.designCreator.model.vo.DesignCreatorRequest;
	import com.designCreator.notifications.NotificationNames;
	
	import flash.events.Event;
	
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.utils.ObjectUtil;
	
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	public class FabricTabContentMediator extends Mediator implements IMediator
	{
		public static const NAME : String = "FabricTabContentMediator";
		
		public function get fabricTabContent() : FabricTabContent
		{
			return viewComponent as FabricTabContent;
		}
		
		public function FabricTabContentMediator( fabricTabContent : FabricTabContent )
		{
			super( NAME, fabricTabContent );
		}
		
		//-------------------------------------------------------------------
		//Overriden methods
		//-------------------------------------------------------------------
		override public function onRegister():void
		{
			fabricTabContent.addEventListener(FabricTabContent.GET_SOLID_FABRIC_DESIGN,getAllSolidFabricDesign);
			fabricTabContent.addEventListener(FabricTabContent.GET_WHITE_FABRIC_DESIGN,getAllWhiteFabricDesign);
			facade.registerCommand( NotificationNames.LOAD_WHITE_FABRICS_DATA, FabricDesignsCommand);
			facade.registerCommand( NotificationNames.LOAD_SOLID_FABRICS_DATA, FabricDesignsCommand);
			//getFabricL1Design();
			//getAllSolidFabricDesign();
			//getAllWhiteFabricDesigdoemnn();
		}
		
		override public function listNotificationInterests() : Array
		{
			return [
				NotificationNames.LOAD_SOLID_FABRIC_DATA_ERROR,
				NotificationNames.LOAD_SOLID_FABRIC_DATA_SUCCESS,
				NotificationNames.LOAD_WHITE_FABRIC_DATA_ERROR,
				NotificationNames.LOAD_WHITE_FABRIC_DATA_SUCCESS
				];
		}
		
		override public function handleNotification( note : INotification ) : void
		{
			switch ( note.getName() )
			{
				case NotificationNames.LOAD_SOLID_FABRIC_DATA_SUCCESS:
					//Alert.show('solid fabric data success:'+ObjectUtil.toString(note.getBody() as XMLListCollection));
					var temp:XMLListCollection = note.getBody() as XMLListCollection ;
					/*if(temp!=null && temp.length>0)
					{
						fabricTabContent.L2DesignComp.L2Designs = temp ;
					}
					else
					{
						fabricTabContent.L2DesignComp.L2Designs = new XMLListCollection();
					}*/
					 
					break;
				
				case NotificationNames.LOAD_SOLID_FABRIC_DATA_ERROR:
					//flexcaster.views.selectedIndex = FEED_VIEW;
					break;
				
				case NotificationNames.LOAD_WHITE_FABRIC_DATA_ERROR:
					//flexcaster.views.selectedIndex = PODCAST_VIEW;
					break;
				case NotificationNames.LOAD_WHITE_FABRIC_DATA_SUCCESS:
					//Alert.show('white fabric data success:'+ObjectUtil.toString(note.getBody() as XMLListCollection));
					break;
			
			}
		}
		
		//-------------------------------------------------------------------
		//private methods
		//-------------------------------------------------------------------
		private function getAllSolidFabricDesign(event:Event=null):void
		{
			var requestVO:DesignCreatorRequest = new DesignCreatorRequest();
			requestVO.solidFabricDesignsXMLPath = FlexGlobals.topLevelApplication.solidFabricDesignXmlPath ;
			sendNotification(NotificationNames.LOAD_SOLID_FABRICS_DATA,requestVO);
		}
	
		private function getAllWhiteFabricDesign(event:Event=null):void
		{
			var requestVO:DesignCreatorRequest = new DesignCreatorRequest();
			requestVO.whiteFabricDesignsXMLPath = FlexGlobals.topLevelApplication.whiteFabricDesignXmlPath ;
			sendNotification(NotificationNames.LOAD_WHITE_FABRICS_DATA,requestVO);
		}
	}
}